package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.ItemReceive;


public class ItemReceiveService {

	Context mContext;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public ItemReceiveService(Context context) {
		this.DBHelper = new DatabaseHelper(context,DatabaseHelper.STOCKDB_NAME);
		this.mContext = context;
	}
	
	public List<ItemReceive> FindAll(){
		List<ItemReceive> obj = new ArrayList<ItemReceive>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.ItemReceive.SCHEMA_NAME,null);;
//		Cursor cursor = DBHelper.rawQuery("select * from "+ DatabaseSchema.BottleSetting.SCHEMA_NAME,null);
		if(cursor == null){
//			MessageBox.show(mContext,DBError.error);
//			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			String upc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_UPC));
			int id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_ID)); 
			int qty  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_QTY));
			double rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_RATE));
			int qty_per_case  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_QTY_PER_CASE));
			String date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_DATE));
			
			obj.add(new ItemReceive(upc,null,qty,qty_per_case,rate,date));
		}
		cursor.close();
		return obj;
	}
	
	
	public ItemReceive Find(String code){
		
		ItemReceive obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.ItemReceive.SCHEMA_NAME +" where upc = "+code,null);
//		Cursor cursor = DBHelper.rawQuery("select * from "+ DatabaseSchema.BottleSetting.SCHEMA_NAME +" where bo_no = "+bo,null);
		
		if(cursor == null){
//			MessageBox.show(mContext,DBError.error);
//			DBError.error = "";
			return null;
		}
		while(cursor.moveToNext()){
			String upc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_UPC));
			int id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_ID)); 
			int qty  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_QTY));
			double rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_RATE));
			int qty_per_case  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_QTY_PER_CASE));
			String date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemReceive.IR_DATE));
			
			obj = new ItemReceive(upc,null,qty,qty_per_case,rate,date);
		}
		cursor.close();
		return obj;	
	}

	public boolean insertInventory(ItemReceive obj){
		boolean check = true;
		String upc,errorString,sql,date;
		int qty,qty_per_case;
		double rate;

		upc = obj.getIr_upc();
		qty = obj.getIr_qty();
		rate = obj.getIr_rate();
		qty_per_case = obj.getIr_qty_per_case();
		date = obj.getIr_date();
		
		sql = "INSERT INTO "+ DatabaseSchema.ItemReceive.SCHEMA_NAME +" values";
		sql += "(null,'"+ upc +"','0',"+ qty+"," + qty_per_case+",null,null," +rate +","+date+",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		
		return check;
	}
	
	public boolean updateStock(ItemReceive obj){
		boolean check = true;
		String upc,errorString,sql,date;
		int qty,qty_per_case;
		double rate;

		upc = obj.getIr_upc();
		qty = obj.getIr_qty();
		rate = obj.getIr_rate();
		qty_per_case = obj.getIr_qty_per_case();
		date = obj.getIr_date();
			
		sql = "UPDATE "+ DatabaseSchema.ItemReceive.SCHEMA_NAME +" set ";
		sql += "upc = '"+ upc +"',qty = "+ qty +",rate =" +rate+"," +
				"qty_per_case = "+ qty_per_case + ",ir_date = '"+date+"',";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		
		return check;
	}
	
}
